﻿@using Compass.Wasm.Shared.Plans
@using Compass.Wasm.Shared.Projects
@using Compass.Wasm.Client.Services.Projects
@using Compass.Wasm.Client.Services.Plans

@inject IProjectService ProjectService
@inject IMainPlanService MainPlanService
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
<tr>
    <td>
        <CompUpdateIcon Roles="admin,pmc,pm" Url="@($"mainplan/update/{MainPlanDto.Id}")"/>
        <CompBatch Batch="MainPlanDto.Batch" />
    </td>
    <td>@MainPlanDto.Number</td>
    <td>
        @if (MainPlanDto.ProjectId!=null)
        {
            <a href="./drawings/@MainPlanDto.ProjectId">@MainPlanDto.Number</a>
            <AuthorizeView Roles="admin,pmc">
                <Authorized>
                    <span class="oi oi-trash text-danger" @onclick="UnbindProject"></span>
                </Authorized>
            </AuthorizeView>
        }
        else
        {
            <a href="mainplan/bind/@MainPlanDto.Id">绑定项目</a>
        }
    </td>
    <td>@MainPlanDto.Name</td>
    <td>@MainPlanDto.Quantity</td>
    <td>@MainPlanDto.ModelSummary</td>
    <td>@MainPlanDto.CreateTime.ToString("MM/dd")</td>
    <td >@MainPlanDto.FinishTime.ToString("MM/dd")</td>
    <td >@MainPlanDto.DrwReleaseTarget.ToString("MM/dd")</td>
    <td style="text-align: center">@MainPlanDto.MainPlanType</td>
</tr>

@if (!string.IsNullOrWhiteSpace(MainPlanDto.Remarks))
{
    <tr>
        <td colspan="10">
            <span style="color: red;font-size:xx-small ">备注：@MainPlanDto.Remarks</span>
        </td>
    </tr>
}

@code {
    [Parameter]
    public MainPlanDto MainPlanDto { get; set; }
    protected override async Task OnParametersSetAsync()
    {
        // if (MainPlanDto.ProjectId != null)
        // {
        //     var response = await ProjectService.GetFirstOrDefault(MainPlanDto.ProjectId.Value);
        //     _project = response.Result;
        // }
    }

    private async Task UnbindProject()
    {
        var confirmResult = await JsRuntime.InvokeAsync<bool>("confirm", $"确定要解除绑定项目【{MainPlanDto.Number}】吗？");
        if (confirmResult)
        {
            MainPlanDto.ProjectId = null;
            MainPlanDto.Status = MainPlanStatus_e.计划;
            //todo:解除绑定
            await MainPlanService.UpdateStatusesAsync(MainPlanDto.Id.Value, MainPlanDto);
        }
    }
}
